<template>
  <el-dialog :title="title" :visible="visible" @close="handleDialogClose" width="600px">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="right"
      label-width="100px"
      class="form"
    >
      <el-form-item label="权限名称:" prop="permissionName">
        <el-input class="form-item" v-model="form.permissionName" placeholder="请输入权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限值:" prop="permissionValue">
        <el-input class="form-item" v-model="form.permissionValue" placeholder="请输入权限值"></el-input>
      </el-form-item>
      <el-form-item label="权限类型:" prop="permissionType">
        <el-select class="form-item" v-model="form.permissionType" placeholder="请选择权限类型">
          <el-option label="目录" :value="1"></el-option>
          <el-option label="菜单" :value="2"></el-option>
          <el-option label="按钮" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.permissionType !== 1"
        :label="form.permissionType === 2 ? '上级目录:' : '上级菜单:'"
        prop="superiorId"
      >
        <el-select
          class="form-item"
          v-model="form.superiorId"
          :placeholder="form.permissionType === 2 ? '请选择上级目录' : '请选择上级菜单'"
        >
          <el-option
            v-for="item of permissionList"
            :key="item.permissionId"
            :label="item.permissionName"
            :value="item.permissionId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="权限状态:">
        <el-radio-group v-model="form.permissionStatus">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import PermissionService from '../../../../service/permission'

export default {
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    // 权限名称验证
    const checkPermissionName = async (rule, value, callback) => {
      if (value.length === 0) {
        return callback(new Error('请输入权限名称'))
      } else {
        const result = await this.getIsExistsByKeyword(value)
        if (result.data) {
          return callback(new Error('该权限名称已被注册'))
        }
        callback()
      }
    }

    // 权限值验证
    const checkPermissionValue = async (rule, value, callback) => {
      if (value.length === 0) {
        return callback(new Error('请输入权限值'))
      } else {
        const resule = await this.getIsExistsByKeyword(value)
        if (resule.data) {
          return callback(new Error('该权限值已被注册'))
        }
        callback()
      }
    }

    return {
      visible: false,
      permissionList: [],
      form: {
        permissionId: '',
        permissionName: '',
        permissionValue: '',
        superiorId: '',
        permissionType: 1,
        permissionStatus: 0
      },
      rules: {
        permissionName: {
          required: true,
          validator: checkPermissionName,
          trigger: 'blur'
        },
        permissionValue: {
          required: true,
          validator: checkPermissionValue,
          trigger: 'blur'
        },
        superiorId: {
          required: true,
          message: '上级目录或上级菜单不能为空',
          trigger: 'change'
        }
      }
    }
  },
  methods: {
    // 处理Dialog关闭事件
    handleDialogClose() {
      this.$emit('update:show')
      this.form = {
        permissionId: '',
        permissionName: '',
        permissionValue: '',
        superiorId: '',
        permissionType: 1,
        permissionStatus: 0
      }
      this.$refs.form.resetFields()
    },
    // 根据关键字查询权限是否存在
    getIsExistsByKeyword(keyword) {
      return PermissionService.getIsExistsByKeyword(
        keyword,
        this.form.permissionId
      )
    },
    // 根据类型查询权限信息
    getPermissionsByType() {
      PermissionService.getPermissionsByType(
        this.form.permissionType - 1,
        this.form.permissionId
      ).then(res => {
        this.permissionList = res.data
      })
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.permissionId) {
            // 清空 children 属性
            delete this.form.children

            PermissionService.updatePermission(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success', null)
            })
          } else {
            PermissionService.insertPermission(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success', 1)
            })
          }
        }
      })
    }
  },
  watch: {
    show(value) {
      this.visible = value
    },
    data(value) {
      this.form = value || {
        permissionId: '',
        permissionName: '',
        permissionValue: '',
        superiorId: '',
        permissionType: 1,
        permissionStatus: 0
      }
    },
    'form.permissionType'(value) {
      if (value === 1) {
        this.form.superiorId = 0
      } else {
        if (this.form.superiorId === 0) {
          this.form.superiorId = ''
        }
        this.getPermissionsByType()
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.form
  margin 0 30px
  .form-item
    width 300px
</style>
